import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import AutoImport from "unplugin-auto-import/vite";
// @ts-ignore # 忽视本行代码的小错误
import postcssPluginPx2rem from "postcss-plugin-px2rem";
import viteCompression from 'vite-plugin-compression'
import { visualizer } from 'rollup-plugin-visualizer'
// https://vitejs.dev/config/
export default defineConfig({
  base:'/',
  resolve: {
    // 配置路径别名
    alias: {
      "@": "/src",
    },
  },
  plugins: [
    react(),
    AutoImport({
      eslintrc:{
        enabled:true,
        filepath:'./.eslintrc-auto-import.json' //此文件为重新启动项目生成的文件
      },
      dts:'./auto-imports.d.ts',//此文件配置保存后系统自动生成
      imports:['react', 'react-router-dom'] //此处可填写需要自动引入的库
    }),
    viteCompression({
      // threshold: 10240 ,
      algorithm: 'gzip',
      // deleteOriginFile: false, // 不删除源文件// 对大于 1mb 的文件进行压缩
    }),
    visualizer({
      open: true,
      filename: 'visualizer.html' //分析图生成的文件名
    }),
  ],
  css: {
    preprocessorOptions: {
      // scss: {
      //   additionalData: `@use "@/styles/index.scss" as *;`,
      // },
    },
    postcss: {
      plugins: [
        postcssPluginPx2rem({
          rootValue: 192,
          unitPrecision: 5,
          mediaQuery: false,
        }),
      ],
    },
  },
  server: {
    port: 8081,
    cors:true,
    proxy: {
      "/blogadmin": {
        target: `http://111.231.15.235:8888`,
        // target: `http://localhost:8888`,

        changeOrigin: true, //需要代理跨域
      },
      // "/xu-file": {
      //   target: `https://xu69.com`,
      //   changeOrigin: true, //需要代理跨域
      // },
    },
  },
  build: {
    sourcemap: false,
    minify: "terser",
    emptyOutDir: true,
    chunkSizeWarningLimit: 1500,
    reportCompressedSize:false,
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output:{
        manualChunks(id) {
      
          if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split("/")[1].toString()
          }
      }
    }
    },
  },
})
